package com.itboy.mall.pms.spuinfo.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.spuinfo.vo.MallSpuInfoVo;
import com.itboy.mall.pms.spuinfo.bo.MallSpuInfoQueryBo;
import com.itboy.mall.pms.spuinfo.bo.MallSpuInfoAddBo;
import com.itboy.mall.pms.spuinfo.bo.MallSpuInfoEditBo;
import com.itboy.mall.pms.spuinfo.service.IMallSpuInfoService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * spu信息Controller
 *
 * @author itboy
 * @date 2021-07-31
 */
@Validated
@Api(value = "spu信息控制器", tags = {"spu信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/spuInfo")
public class MallSpuInfoController extends BaseController {

    private final IMallSpuInfoService iMallSpuInfoService;

    /**
     * 查询spu信息列表
     */
    @ApiOperation("查询spu信息列表")
    @PreAuthorize("@ss.hasPermi('mall:spuInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<MallSpuInfoVo> list(@Validated MallSpuInfoQueryBo bo) {
        return iMallSpuInfoService.queryPageList(bo);
    }

    /**
     * 导出spu信息列表
     */
    @ApiOperation("导出spu信息列表")
    @PreAuthorize("@ss.hasPermi('mall:spuInfo:export')")
    @Log(title = "spu信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallSpuInfoVo> export(@Validated MallSpuInfoQueryBo bo) {
        List<MallSpuInfoVo> list = iMallSpuInfoService.queryList(bo);
        ExcelUtil<MallSpuInfoVo> util = new ExcelUtil<MallSpuInfoVo>(MallSpuInfoVo.class);
        return util.exportExcel(list, "spu信息");
    }

    /**
     * 获取spu信息详细信息
     */
    @ApiOperation("获取spu信息详细信息")
    @PreAuthorize("@ss.hasPermi('mall:spuInfo:query')")
    @GetMapping("/{id}")
    public AjaxResult<MallSpuInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
		return AjaxResult.success(iMallSpuInfoService.queryInfoById(id));
    }

    /**
     * 新增spu信息
     */
    @ApiOperation("新增spu信息")
    @PreAuthorize("@ss.hasPermi('mall:spuInfo:add')")
    @Log(title = "spu信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallSpuInfoAddBo bo) {
        return toAjax(iMallSpuInfoService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改spu信息
     */
    @ApiOperation("修改spu信息")
    @PreAuthorize("@ss.hasPermi('mall:spuInfo:edit')")
    @Log(title = "spu信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallSpuInfoEditBo bo) {
        return toAjax(iMallSpuInfoService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除spu信息
     */
    @ApiOperation("删除spu信息")
    @PreAuthorize("@ss.hasPermi('mall:spuInfo:remove')")
    @Log(title = "spu信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMallSpuInfoService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

	/**
	 * 上架
	 */
	@ApiOperation("上架")
	@Log(title = "上架", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/up/{id}")
	public AjaxResult<Void> up(@NotNull(message = "主键不能为空")
										@PathVariable("id") Long id) {
		return toAjax(iMallSpuInfoService.up(id) ? 1 : 0);
	}
}
